<template>
  <div class="content">
    <div class="filter">
      <el-row>
        <el-col :span="21">
          <headline v-bind:title="title"></headline>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" icon="el-icon-plus" class="tian" @click="tianjia()">添加品类</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="data-table">
      <el-table :data="list.slice((pageNum - 1) * pagesize, pageNum * pagesize)" border style="width:100%"
                max-height="500">
        <el-table-column prop="id" label="品类id"></el-table-column>
        <el-table-column prop="name" label="品类名称"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="xiugai(scope.row)">修改名称</el-button>
            <el-button type="text" size="small" @click="chakan(scope.row)">查看其子品类</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[10, 20, 40]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="list.length">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import headline from "../headline.vue";

export default {
  components: {headline},
  data() {
    return {
      title: "品类管理",
      list: [],
      default: 0,
      pageNum: 1,
      pagesize: 10,
    }
  },
  mounted() {
    this.pinglei()
  },
  methods: {
    handleSizeChange: function (size) {
      this.pagesize = size;
    },
    handleCurrentChange: function (pageNum) {
      this.pageNum = pageNum;
    },
    //品类数据
    pinglei() {
      let postData = this.qs.stringify({categoryId: this.default})
      this.axios({method: 'POST', url: '/api/manage/category/get_category.do', data: postData}).then(res => {
        this.list = res.data.data
      })
    },
    //修改名称
    xiugai(row) {
      this.$prompt('请输入新的品类名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        let postData = this.qs.stringify({categoryId: row.id, categoryName: value})
        this.axios({method: 'POST', url: '/api/manage/category/set_category_name.do', data: postData}).then(res => {
          this.pinglei()
          this.$message({type: 'success', message: res.data.data})
        })
      }).catch(() => {
        this.$message({type: 'info', message: '取消输入'})
      })
    },
    chakan(row) {
      this.$router.push({name: "songory", params: {id: row.id}})
    },
    tianjia() {
      this.$router.push({path: 'addgory'})
    }
  }
}
</script>
<style scoped lang="less">
.block {
  margin-top: 15px;
  background: white;
  margin-bottom: 10px;
}
</style>